package com.cvte.vdt.uartservice.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

import com.cvte.vdtlibrary.bluetooth.EntityBluetoothDevice;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wuguitong on 2018/11/28.
 */
public class BtSharePreferencesUtil {
    private final static String FILENAME = "BtDevData";
    private final static String BT_SEND_MODE_ONOFF = "bt_send_mode_onoff";
    private final static boolean DEF_BT_SEND_MODE_ONOFF = false;
    public static boolean saveBtSendOnOff(Context context,boolean isBtOn){
        SharedPreferences preferences = context.getSharedPreferences(FILENAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(BT_SEND_MODE_ONOFF,isBtOn);
        editor.commit();
        return true;
    }
    public static boolean getBtSendModOnOff(Context context){
        SharedPreferences preferences = context.getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        return preferences.getBoolean(BT_SEND_MODE_ONOFF,DEF_BT_SEND_MODE_ONOFF);
    }
    //not use below use sqlite
    public final static String DEFNAME = "";
    public static boolean saveBtDev(Context context,String key, String name){
        SharedPreferences preferences = context.getSharedPreferences(FILENAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        if(!preferences.contains(key) || (preferences.contains(key) && !preferences.getString(key,DEFNAME).equals(name))){
            editor.putString(key,name);
            editor.commit();
        }
        return true;
    }
    public static List<EntityBluetoothDevice> getBtDevList(Context context){
        List<EntityBluetoothDevice> list = new ArrayList<EntityBluetoothDevice>();
        SharedPreferences preferences = context.getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        Map<String,String> map = (Map<String, String>) preferences.getAll();
        for(Map.Entry<String,String> entry:map.entrySet()){
            EntityBluetoothDevice dev = new EntityBluetoothDevice(entry.getKey(),entry.getValue());
            list.add(dev);
        }
        return list;
    }
    public static boolean cleanBtDev(Context context){
        SharedPreferences preferences = context.getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        preferences.edit().clear().commit();
        return true;
    }
    public static boolean removeBtDev(Context context,String key){
        SharedPreferences preferences = context.getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        preferences.edit().remove(key).commit();
        return true;
    }
    public static String getBtDevName(Context context,String key){
        SharedPreferences preferences = context.getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        return preferences.getString(key,DEFNAME);
    }
    public static boolean isContainBtDev(Context context,String key){
        SharedPreferences preferences = context.getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        return preferences.contains(key);
    }
}
